package com.yanqu.road.server.gameplayer.module.activity.zhenhuan;

import com.yanqu.road.dao.impl.player.UserZhenHuanThirdProcessDaoImpl;
import com.yanqu.road.entity.activity.zhenhuan.thirdProcess.UserZhenHuanThirdProcessData;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.logic.activity.zhenhuan.ZhenHuanThirdProcessConfig;
import com.yanqu.road.logic.helper.ActivityHelper;
import com.yanqu.road.pb.activity.ZhenHuanProto;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.ActivityWorshipModule;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.zhenhuan.ZhenHuanConfigMgr;
import com.yanqu.road.server.manger.activity.zhenhuan.ZhenHuanMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.pb.PlayerPb;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

public class ZhenHuanThirdProcessModule extends GeneralModule {
    public ZhenHuanThirdProcessModule(GamePlayer player) {
        super(player);
    }

    private UserZhenHuanThirdProcessData userData;

    @Override
    public boolean loadData() {
        ZhenHuanThirdProcessConfig thirdProcessConfig = ZhenHuanConfigMgr.getThirdProcessConfig();
        if (thirdProcessConfig != null) {
            userData = new UserZhenHuanThirdProcessDaoImpl().getUserZhenHuanThirdProcessData(thirdProcessConfig.getActivityId(), getUserId());
        }

        return true;
    }

    @Override
    public boolean afterLoadData() {
        return true;
    }

    @Override
    public boolean saveData() {
        if (userData != null) {
            if (userData.isInsertOption()) {
                new UserZhenHuanThirdProcessDaoImpl().add(userData);
            } else if (userData.isUpdateOption()) {
                new UserZhenHuanThirdProcessDaoImpl().update(userData);
            }
        }
        return true;
    }

    @Override
    public void afterLogin() {
        iniSystem();
    }

    private void initUserData() {
        if (!SystemOpenMgr.systemOpen(player, eSystemId.ZhenHuanThirdProcess.getValue())) {
            return;
        }
        ZhenHuanThirdProcessConfig thirdProcessConfig = ZhenHuanConfigMgr.getThirdProcessConfig();
        if (thirdProcessConfig == null) {
            return;
        }
        if (userData == null || userData.getActivityId() != thirdProcessConfig.getActivityId()) {
            userData = new UserZhenHuanThirdProcessData(thirdProcessConfig.getActivityId(), getUserId());
        }

    }

    private void syncConfig() {

    }

    private void syncUserData() {
        if (userData == null) {
            return;
        }
        ZhenHuanThirdProcessConfig config = ZhenHuanConfigMgr.getThirdProcessConfig();
        if (config == null) {
            return;
        }
        ZhenHuanProto.CrossZhenHuanThirdProcessGetSelfDataReqMsg.Builder reqMsg = ZhenHuanProto.CrossZhenHuanThirdProcessGetSelfDataReqMsg.newBuilder();
        reqMsg.setActivityId(config.getActivityId());
        reqMsg.setIsWorship(player.getModule(ActivityWorshipModule.class).isWorship(config.getActivityId()));
        player.sendPacket(Protocol.C_CROSS_ZHENHUAN_THIRD_PROCESS_SELF_DATA_SYNC, reqMsg);
    }


    public void iniSystem() {
        if (!SystemOpenMgr.systemOpen(player, eSystemId.ZhenHuanThirdProcess.getValue())) {
            return;
        }
        initUserData();
        syncConfig();
        syncUserData();
    }

    /**
     * 开宴会
     */
    public int openBanquet(int activityId, int openItemId) {
        ZhenHuanThirdProcessConfig config = ZhenHuanConfigMgr.getThirdProcessConfig();
        if (config == null) {
            return GameErrorCode.E_ZHENHUAN_THIRD_PROCESS_CONFIG_NOT_FOUND;
        }
        if (!ActivityHelper.activityInTime(config.getActivityInfo())) {
            return GameErrorCode.E_ZHENHUAN_THIRD_PROCESS_NOT_INT_TIME;
        }
        if (config.getActivityId() != activityId) {
            return GameErrorCode.E_ZHENHUAN_THIRD_PROCESS_NOT_INT_TIME;
        }

        //判断消耗道具ID是的正确
        if (!config.checkCostItemId(openItemId)) {
            return GameErrorCode.E_ZHENHUAN_THIRD_PROCESS_OPEN_BANQUET_PROP_ERROR;
        }

        boolean activityAble = ZhenHuanMgr.isActivityAble(getUserId(), config.getActivityId());
        if (!activityAble) {
            return GameErrorCode.E_ZHENHUAN_THIRD_PROCESS_USER_NOT_FOUND;
        }

        // 拿一下消耗
        Property cost = new Property(openItemId, 1);
        if (!player.getModule(CurrencyModule.class).currencyIsEnough(cost)) {
            return GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
        }
        // 预扣除
        player.getModule(PreReduceModule.class).preReduce(cost);

        // 上跨服
        ZhenHuanProto.CrossZhenHuanThirdProcessOpenBanquetReqMsg.Builder reqMsg = ZhenHuanProto.CrossZhenHuanThirdProcessOpenBanquetReqMsg.newBuilder();
        reqMsg.setActivityId(activityId);
        reqMsg.setPlayerBaseData(PlayerPb.parsePlayerBaseTempMsg(player.getUserInfo()));
        reqMsg.setOpenItemId(openItemId);
        player.sendPacket(Protocol.C_CROSS_ZHENHUAN_THIRD_PROCESS_OPEN_BANQUET, reqMsg);
        return 0;
    }

    /**
     * 加入宴会
     *
     * @param activityId   活动id
     * @param targetUserId 谁的宴会
     * @param banquetId    宴会id
     */
    public int joinBanquet(int activityId, long targetUserId, int banquetId) {
        if (userData == null) {
            return GameErrorCode.E_SYSTEM_NO_OPEN;
        }
        ZhenHuanThirdProcessConfig config = ZhenHuanConfigMgr.getThirdProcessConfig();
        if (config == null) {
            return GameErrorCode.E_ZHENHUAN_THIRD_PROCESS_CONFIG_NOT_FOUND;
        }
        if (!ActivityHelper.activityInShowTime(config.getActivityInfo())) {
            return GameErrorCode.E_ZHENHUAN_THIRD_PROCESS_NOT_INT_TIME;
        }
        if (config.getActivityId() != activityId) {
            return GameErrorCode.E_ZHENHUAN_THIRD_PROCESS_NOT_INT_TIME;
        }

        // 上跨服
        ZhenHuanProto.CrossZhenHuanThirdProcessJoinBanquetReqMsg.Builder reqMsg = ZhenHuanProto.CrossZhenHuanThirdProcessJoinBanquetReqMsg.newBuilder();
        reqMsg.setActivityId(activityId);
        reqMsg.setTargetUserId(targetUserId);
        reqMsg.setBanquetId(banquetId);
        reqMsg.setPlayerBaseData(PlayerPb.parsePlayerBaseTempMsg(player.getUserInfo()));
        player.sendPacket(Protocol.C_CROSS_ZHENHUAN_THIRD_PROCESS_JOIN_BANQUET, reqMsg);
        return 0;
    }

    /**
     * 拉取用户的宴会列表
     *
     * @param activityId   活动id
     * @param targetUserId 谁的宴会
     */
    public int getUserBanquetList(int activityId, long targetUserId) {
        ZhenHuanThirdProcessConfig config = ZhenHuanConfigMgr.getThirdProcessConfig();
        if (config == null) {
            return GameErrorCode.E_ZHENHUAN_THIRD_PROCESS_CONFIG_NOT_FOUND;
        }
        if (!ActivityHelper.activityInShowTime(config.getActivityInfo())) {
            return GameErrorCode.E_ZHENHUAN_THIRD_PROCESS_NOT_INT_TIME;
        }
        if (config.getActivityId() != activityId) {
            return GameErrorCode.E_ZHENHUAN_THIRD_PROCESS_NOT_INT_TIME;
        }

        // 上跨服
        ZhenHuanProto.CrossZhenHuanThirdProcessGetUserBanquetListReqMsg.Builder reqMsg = ZhenHuanProto.CrossZhenHuanThirdProcessGetUserBanquetListReqMsg.newBuilder();
        reqMsg.setActivityId(activityId);
        reqMsg.setTargetUserId(targetUserId);
        player.sendPacket(Protocol.C_CROSS_ZHENHUAN_THIRD_PROCESS_GET_USER_BANQUET_LIST, reqMsg);
        return 0;
    }

    /**
     * 拉取宴会成员列表
     *
     * @param activityId   活动id
     * @param targetUserId 宴会开启者
     * @param banquetId    宴会id
     * @param page         第几页
     * @param num          每页人数
     */
    public int getBanquetJoinerList(int activityId, long targetUserId, int banquetId, int page, int num) {
        ZhenHuanThirdProcessConfig config = ZhenHuanConfigMgr.getThirdProcessConfig();
        if (config == null) {
            return GameErrorCode.E_ZHENHUAN_THIRD_PROCESS_CONFIG_NOT_FOUND;
        }
        if (!ActivityHelper.activityInShowTime(config.getActivityInfo())) {
            return GameErrorCode.E_ZHENHUAN_THIRD_PROCESS_NOT_INT_TIME;
        }
        if (config.getActivityId() != activityId) {
            return GameErrorCode.E_ZHENHUAN_THIRD_PROCESS_NOT_INT_TIME;
        }

        // 上跨服
        ZhenHuanProto.CrossZhenHuanThirdProcessGetBanquetJoinerListReqMsg.Builder reqMsg = ZhenHuanProto.CrossZhenHuanThirdProcessGetBanquetJoinerListReqMsg.newBuilder();
        reqMsg.setActivityId(activityId);
        reqMsg.setTargetUserId(targetUserId);
        reqMsg.setBanquetId(banquetId);
        reqMsg.setPage(page);
        reqMsg.setNum(num);
        player.sendPacket(Protocol.C_CROSS_ZHENHUAN_THIRD_PROCESS_GET_BANQUET_JOINER_LIST, reqMsg);
        return 0;
    }

    /**
     * 获取所有参赛者信息
     *
     * @param activityId   活动id
     * @param targetUserId 请求某个人或者全部传0
     */
    public int getAllUser(int activityId, long targetUserId) {
        ZhenHuanThirdProcessConfig config = ZhenHuanConfigMgr.getThirdProcessConfig();
        if (config == null) {
            return GameErrorCode.E_ZHENHUAN_THIRD_PROCESS_CONFIG_NOT_FOUND;
        }
        if (!ActivityHelper.activityInShowTime(config.getActivityInfo())) {
            return GameErrorCode.E_ZHENHUAN_THIRD_PROCESS_NOT_INT_TIME;
        }
        if (config.getActivityId() != activityId) {
            return GameErrorCode.E_ZHENHUAN_THIRD_PROCESS_NOT_INT_TIME;
        }

        // 上跨服
        ZhenHuanProto.CrossZhenHuanThirdProcessGetAllUserReqMsg.Builder reqMsg = ZhenHuanProto.CrossZhenHuanThirdProcessGetAllUserReqMsg.newBuilder();
        reqMsg.setActivityId(activityId);
        reqMsg.setTargetUserId(targetUserId);
        player.sendPacket(Protocol.C_CROSS_ZHENHUAN_THIRD_PROCESS_GET_ALL_USER, reqMsg);
        return 0;
    }

    public void updateLastJoinTime(long currentTime) {
        if (userData == null) {
            return;
        }
        userData.setLastJoinTime(currentTime);
    }
}
